<script setup lang="ts">
import { TreeKey } from 'element-plus/es/components/tree/src/tree.type'
import { useDataScopeForm } from './hook'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
  },
  deptOptions: {
    type: Array,
  },
  checkedKeys: {
    type: Array,
  },
  data: {
    type: Object,
    default: () => {
      return {}
    },
  },
})

const emit = defineEmits(['update:visible', 'refresh'])
const {
  title,
  formData,
  deptRef,
  deptExpand,
  deptOptions,
  deptNodeAll,
  formVisible,
  dataScopeOptions,
  sys_normal_disable,
  closeDialog,
  submitDataScope,
  dataScopeSelectChange,
  handleCheckedTreeExpand,
  handleCheckedTreeNodeAll,
  handleCheckedTreeConnect,
} = useDataScopeForm(props, emit)
</script>

<template>
  <el-dialog
    v-model="formVisible"
    :title="title"
    :width="500"
    draggable
    :before-close="closeDialog"
  >
    <!-- 表单内容 -->
    <el-form :model="formData" label-width="100px">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="formData.roleName" placeholder="请输入角色名称" disabled />
      </el-form-item>
      <el-form-item label="权限字符" prop="roleKey">
        <el-input v-model="formData.roleKey" placeholder="请输入权限字符" disabled />
      </el-form-item>
      <el-form-item label="权限范围">
        <el-select v-model="formData.dataScope" @change="dataScopeSelectChange">
          <el-option
            v-for="item in dataScopeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formData.status">
          <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{
            dict.label
          }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-show="formData.dataScope == 2" label="数据权限">
        <el-checkbox v-model="deptExpand" @change="handleCheckedTreeExpand($event, 'dept')"
          >展开/折叠</el-checkbox
        >
        <el-checkbox v-model="deptNodeAll" @change="handleCheckedTreeNodeAll($event, 'dept')"
          >全选/全不选</el-checkbox
        >
        <el-checkbox
          v-model="formData.deptCheckStrictly"
          @change="handleCheckedTreeConnect($event, 'dept')"
          >父子联动</el-checkbox
        >
        <el-tree
          ref="deptRef"
          class="border w-full border-gray-200 mt-2 rounded bg-white"
          :data="deptOptions"
          show-checkbox
          default-expand-all
          node-key="id"
          :check-strictly="!formData.deptCheckStrictly"
          :default-checked-keys="checkedKeys as TreeKey[]"
          empty-text="加载中，请稍候"
          :props="{ label: 'label', children: 'children' }"
        />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="submitDataScope"> 确定 </el-button>
    </template>
  </el-dialog>
</template>
